package cn.sdormitory.canteen.service.impl;

import cn.sdormitory.canteen.dao.CanteenBuyDao;
import cn.sdormitory.canteen.service.CanteenBuyService;
import cn.sdormitory.canteen.service.CanteenStockDetailService;
import cn.sdormitory.canteen.vo.CanteenBuyVO;
import cn.sdormitory.canteen.vo.CanteenStockDetailVO;
import cn.sdormitory.common.api.CommonPage;
import cn.sdormitory.school.dao.SchoolDutyDao;
import cn.sdormitory.school.entity.SchoolDuty;
import cn.sdormitory.smartclass.vo.ClassDvVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("canteenBuyService")
public class CanteenBuyServiceImpl extends ServiceImpl<CanteenBuyDao, CanteenBuyVO> implements CanteenBuyService {

    @Autowired
    private CanteenBuyDao canteenBuyDao;
    @Autowired
    private CanteenStockDetailService canteenStockDetailService;

    @Override
    public CommonPage<CanteenBuyVO> getPage(Map<String, Object> params) {
        int pageSize = Integer.parseInt(String.valueOf(params.get("pageSize")));
        int pageNum = Integer.parseInt(String.valueOf(params.get("pageNum")));

        List<CanteenBuyVO> list = this.canteenBuyDao.getCanteenBuyList(params, (pageNum - 1) * pageSize, pageSize);

        CommonPage commonPage = CommonPage.getCommonPage(pageNum, pageSize, this.canteenBuyDao.getCanteenBuyListCount(params), list);

        return commonPage;
    }

    @Override
    public int createOrUpdateCanteenBuyInfo(CanteenBuyVO vo) {
        int num=0;
        if(StringUtils.isEmpty(vo.getId())){
            vo.setCreatedTime(new Date());
            num = canteenBuyDao.insert(vo);
        }else{
            num = canteenBuyDao.updateById(vo);
        }
        return num;
    }

    @Override
    public int deleteCanteenBuyById(Long id) {
        return canteenBuyDao.deleteById(id);
    }

    @Override
    public int goodsPutStock(CanteenBuyVO vo) {
        int num = 0;
        if(!StringUtils.isEmpty(vo.getId())){
            //修改采购信息
            canteenBuyDao.updateById(vo);
            //添加入库信息
            CanteenStockDetailVO stockVO = new CanteenStockDetailVO();
            stockVO.setCanteenBuyId(vo.getId());
            stockVO.setInOutStatus("1");
            stockVO.setInOutNumber(vo.getGoodsNumber());
            num = canteenStockDetailService.stockAdd(stockVO);
        }
        return num;
    }
}
